<?php

class ProgramsController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/admin/main';

  public function init(){
        Yii::app()->user->loginUrl = array('/admin/site/login');
        parent::init();
    }

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'new', 'edit', 'delete'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
		$program = Programs::model()->find('id = :id', array(':id' => $_GET['id']));
    // $users = Users::model()->findAll('program_id = :program_id', array(':program_id' => $program->id));

    $class_name = ucfirst(strtolower($program->name));
    $model = CActiveRecord::model($class_name)->findAll();

		$this->render('index',array(
      'program' => $program,
      // 'users' => $users,
			'models' => $model,
		));
	}

  public function actionView($id)
  {
    $program_with_details_id = array('4');
    $program_with_details_name = array('ActivitiesDetail');
    
    $program_id = $_GET['program'];
    $details = NULL;
  
    $program = Programs::model()->find('id = :id', array(':id' => $_GET['program']));
    $class_name = ucfirst(strtolower($program->name));
    $model = CActiveRecord::model($class_name)->find('id = :id', array(':id' => $id));

    $fields = array_keys($model->attributes);
    
    // programs with detail table
    if(in_array($program_id, $program_with_details_id) == true){
      // search index
      $index = array_search($program_id, $program_with_details_id);
      $details = CActiveRecord::model($program_with_details_name[$index])->findAll('activity_id = :id', array(':id' => $model->id));
    }

    $this->render('view',array(
      'program' => $program,
			'model' => $model,
      'fields' => $fields,
      'model_name' => $class_name,
      'details' => $details,
		));
  }

	public function actionDelete()
	{
    $program_with_photo = array('3');
    $program_with_details_id = array('4');
    $program_with_details_name = array('ActivitiesDetail');
  
    $id = $_GET['id'];
    $program_id = $_GET['program'];
    
    $program = Programs::model()->find('id = :id', array(':id' => $program_id));
    $class_name = ucfirst(strtolower($program->name));
    $model = CActiveRecord::model($class_name)->find('id = :id', array(':id' => $id));
    if($model->delete()){
    // if($model){
      
      // program with photo
      if(in_array($program_id, $program_with_photo) == true){
        $path=Yii::app()->basePath. '/../assets/users/'. $class_name . '/' . $model->photo;
        unlink($path);
      }
      
      // program with other detail table
      if(in_array($program_id, $program_with_details_id) == true){
        // search index
        $index = array_search($program_id, $program_with_details_id);
      
        $details = CActiveRecord::model($program_with_details_name[$index])->findAll('activity_id = :id', array(':id' => $model->id));
        foreach($details as $detail){
          $detail->delete();
        }
      }
    
      Yii::app()->user->setFlash('success', 'Success');
      Yii::app()->user->setFlash('content', 'You have successfully deleted a member.');

      $this->redirect(bu('admin/programs?id='.$_GET['program']));
    }

    Yii::app()->user->setFlash('error', 'Error');
    Yii::app()->user->setFlash('content', 'Delete unsuccessfull. Please try again.');
	}

  public function actionEdit($id)
  {
    $program=Programs::model()->find('id = :id', array(':id'=>$id));
    $this->render('edit', array(
      'program' => $program,
      'id' => $id,
    ));
  }

  public function actionUpdate()
  {
    $id=$_POST['type'];
    $program=Programs::model()->find('id = :id', array(':id'=>$id));

    if(isset($_POST['Programs']))
    {
      $tmp = $program->logo;
      $data = $_POST['Programs'];
      $program->attributes = $data;

      if (isset($data['upload']) && $data['upload'] != NULL)
      {
        $program->logo = CUploadedFile::getInstance($program,'upload');
      }
      else
      {
        $program->logo = $tmp;
      }

      if($program->save())
      {
        if (isset($data['upload']) && $data['upload'] != NULL)
        {
          $program->logo->saveAs(Yii::app()->basePath. '/../assets/programs/' . $program->logo);
        }

        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have successfully edit your program info.');

				$this->redirect(array('index?id='.$_POST['type']));
      }
    }

    $this->render('edit', array(
      'program' => $program,
      'id' => $id,
    ));
  }
}
